using Microsoft.AspNetCore.Mvc;
using RiceShop.Models;
using RiceShop.Services;

namespace RiceShop.Controllers.wx
{
    /// <summary>
    /// 商品品牌控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class BrandController : ControllerBase
    {
        private readonly IBrandService _brandService;

        public BrandController(IBrandService brandService)
        {
            _brandService = brandService;
        }

        /// <summary>
        /// 获取所有品牌列表
        /// </summary>
        /// <returns>品牌列表</returns>
        [HttpGet("list")]
        public async Task<ActionResult<IEnumerable<Brand>>> GetBrands()
        {
            try
            {
                var brands = await _brandService.GetBrandsAsync();
                return Ok(ApiResponse<IEnumerable<Brand>>.Ok(brands, "获取品牌列表成功"));
            }
            catch (Exception ex)
            {
                return StatusCode(500, ApiResponse<IEnumerable<Brand>>.Error($"获取品牌列表失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 根据ID获取品牌详情
        /// </summary>
        /// <param name="id">品牌ID</param>
        /// <returns>品牌详情</returns>
        [HttpGet("detail/{id}")]
        public async Task<ActionResult<Brand>> GetBrand(long id)
        {
            try
            {
                var brand = await _brandService.GetBrandByIdAsync(id);
                if (brand == null)
                {
                    return NotFound(ApiResponse<Brand>.Error("未找到指定品牌"));
                }
                return Ok(ApiResponse<Brand>.Ok(brand, "获取品牌详情成功"));
            }
            catch (Exception ex)
            {
                return StatusCode(500, ApiResponse<Brand>.Error($"获取品牌详情失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 创建新品牌
        /// </summary>
        /// <param name="brand">品牌信息</param>
        /// <returns>创建的品牌信息</returns>
        [HttpPost("create")]
        public async Task<ActionResult<Brand>> CreateBrand([FromBody] Brand brand)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ApiResponse<Brand>.Error("无效的请求数据"));
                }

                brand.CreateTime = DateTime.Now;
                brand.UpdateTime = DateTime.Now;
                brand.IsDeleted = false;

                var createdBrand = await _brandService.CreateBrandAsync(brand);
                return CreatedAtAction(
                    nameof(GetBrand),
                    new { id = createdBrand.Id },
                    ApiResponse<Brand>.Ok(createdBrand, "创建品牌成功")
                );
            }
            catch (Exception ex)
            {
                return StatusCode(500, ApiResponse<Brand>.Error($"创建品牌失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 更新品牌信息
        /// </summary>
        /// <param name="id">品牌ID</param>
        /// <param name="brand">更新的品牌信息</param>
        /// <returns>更新结果</returns>
        [HttpPut("update/{id}")]
        public async Task<ActionResult<Brand>> UpdateBrand(long id, [FromBody] Brand brand)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ApiResponse<Brand>.Error("无效的请求数据"));
                }

                if (id != brand.Id)
                {
                    return BadRequest(ApiResponse<Brand>.Error("请求路径ID与品牌ID不匹配"));
                }

                brand.UpdateTime = DateTime.Now;
                var result = await _brandService.UpdateBrandAsync(brand);
                if (!result)
                {
                    return NotFound(ApiResponse<Brand>.Error("未找到要更新的品牌"));
                }

                return Ok(ApiResponse<Brand>.Ok(brand, "更新品牌成功"));
            }
            catch (Exception ex)
            {
                return StatusCode(500, ApiResponse<Brand>.Error($"更新品牌失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 删除品牌
        /// </summary>
        /// <param name="id">品牌ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("delete/{id}")]
        public async Task<ActionResult> DeleteBrand(long id)
        {
            try
            {
                var result = await _brandService.DeleteBrandAsync(id);
                if (!result)
                {
                    return NotFound(ApiResponse<object>.Error("未找到要删除的品牌"));
                }
                return Ok(ApiResponse<object>.Ok(null, "删除品牌成功"));
            }
            catch (Exception ex)
            {
                return StatusCode(500, ApiResponse<object>.Error($"删除品牌失败: {ex.Message}"));
            }
        }
    }
}